import * as types from '../constants/ActionTypes';
import Request from '../utils/request';

// 业绩接口
const fetchPerformance = () => (dispatch) => {
  Request.get({
    url: `/dealer/saleperformance?app=${1}`,
    accept: 'text',
    success: (data) => {
      dispatch(performance(data));
    },
    fail: () => {
      dispatch(performance(''));
    },
    error: () => {
      dispatch(performance(''));
    },
  });
};

// 厂商接口
const fetchFirm = (factoryId, cb) => {
  Request.get({
    url: `/dealer/switch/factory/${factoryId}?app=${1}`,
    accept: 'text',
    success: (data) => {
      if (data.ok === 1) {
        if (cb) {
          cb();
        }
      }
    },
    fail: () => {
      if (cb) {
        cb({
          err: '更换厂商失败！',
        });
      }
    },
    error: () => {
      if (cb) {
        cb({
          err: '更换厂商失败！',
        });
      }
    },
  });
};

const performance = PerformanceData => ({
  type: types.MYS_PERFORMANCE,
  PerformanceData,
});

export {
  fetchPerformance,
  fetchFirm,
};
